//
//  downFile.m
//  HappyStore
//
//  Created by 李小辉 on 16/6/23.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "downFile.h"
#import "downFileCell.h"
#import <QuickLook/QuickLook.h>
@interface downFile ()<QLPreviewControllerDataSource,QLPreviewControllerDelegate>
@property (strong, nonatomic) NSString *documentsPath;//document的路径
@property (strong, nonatomic) NSMutableArray *docFileAry;
@property (strong, nonatomic) NSString *docName;
@property (strong, nonatomic) QLPreviewController *qlview;
@end

static NSString *downfilecell = @"downFileCell";

@implementation downFile

- (NSString *)documentsPath{
    if (!_documentsPath) {
        _documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    }
    return _documentsPath;
}



- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"下载目录";
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    
    NSString *fileName = [self.documentsPath stringByAppendingPathComponent:@"docFile.plist"];
    self.docFileAry = [NSMutableArray arrayWithContentsOfFile:fileName];
    //注册
    UINib *nib = [UINib nibWithNibName:@"downFileCell" bundle:nil];
   [self.tableView registerNib:nib forCellReuseIdentifier:downfilecell];
    //去掉多余的表格线
    [self setExtraCellLineHidden:self.tableView];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
-(void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setExtraCellLineHidden:(UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:self.docFileAry.count];
    return self.docFileAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 92;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    downFileCell *cell = [tableView dequeueReusableCellWithIdentifier:downfilecell];
    NSDictionary *dicData = self.docFileAry[indexPath.row];
    cell.titleLabel.text = dicData[@"name"];
    cell.timeLabel.text = dicData[@"downTime"];
    if ([dicData[@"type"] isEqualToString:@"投资政策"]) {
        cell.leftImageView.image = [UIImage imageNamed:@"投资政策_down"];
    }else if ([dicData[@"type"] isEqualToString:@"合同范本"]){
        cell.leftImageView.image = [UIImage imageNamed:@"合同范本_down"];
    }else if ([dicData[@"type"] isEqualToString:@"政策法规"]){
        cell.leftImageView.image = [UIImage imageNamed:@"政策法规_down"];
    }else{
        cell.leftImageView.image = [UIImage imageNamed:@"gongkainianbao"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    //取消选择状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.docFileAry[indexPath.row];
    self.docName = dic[@"filePath"];
    if (self.docName) {
        self.qlview = [[QLPreviewController alloc] init];
        self.qlview.dataSource = self;
        self.qlview.delegate = self;
        self.qlview.currentPreviewItemIndex = 0;
        [self.navigationController pushViewController:self.qlview animated:YES];
    }
    
    
    
}



#pragma mark - 表格侧滑－－删除

//侧滑－－删除
//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
    
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


//进入编辑模式，按下出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self deleteFile:indexPath];
    
}
//编辑的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}



#pragma mark - QLPreview- data source

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    
    NSURL *myQLDocument = [NSURL fileURLWithPath:self.docName];
    return myQLDocument;
}

#pragma mark - 解决表格线不到左边界
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}


- (void)viewDidLayoutSubviews{
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
}

//删除文件
- (void)deleteFile:(NSIndexPath *)indexPath{
    
    NSDictionary *dic = self.docFileAry[indexPath.row];
    NSString *strPath = dic[@"filePath"];
    NSString *fileName = [self.documentsPath stringByAppendingPathComponent:@"docFile.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //判断文件是否存在
    BOOL blHabe = [fileManager fileExistsAtPath:strPath];
    
    if (blHabe) {//存在
        BOOL biDele = [fileManager removeItemAtPath:strPath error:nil];
        if (biDele) {
            [self.docFileAry removeObject:dic];
            [self.docFileAry writeToFile:fileName atomically:YES];
            [self.tableView reloadData];
        }else{
            [self showErrorWithTitle:@"删除失败" autoCloseTime:2];
        }
        
    }else{//不存在
        [self.docFileAry removeObject:dic];
        [self.docFileAry writeToFile:fileName atomically:YES];
        [self.tableView reloadData];
    }
    
    
}



@end
